<?php

namespace App\Http\Controllers\Book;

use App\Http\Controllers\BaseController;
use App\Models\Book;
use Illuminate\Http\Request;

class BookController extends BaseController
{
    public function index(Request $request)
    {
        $param = $this->validate($request,[
            'keyword' => 'string|max:100'
        ]);

        $query = Book::query()->where('status',1);

        if (isset($param['keyword'])){
            $query->where('title','like','%'.$param['keyword'].'%');
        }

       $data =  $query->orderBy('sort')->orderByDesc('id')->simplePaginate();

       return $this->success($data);
    }

    public function show(Request $request)
    {
        $param = $this->validate($request,[
            'book_id' => 'required'
        ]);
        $data = Book::query()
                    ->withCount('comment')
                    ->where('id',$param['book_id'])->first();

        $data->real_view_count++;
        $data->save();

        return $this->success($data);
    }
}
